package com.cc.proxy;

import com.cc.utils.SqlSessionUtils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.ibatis.session.SqlSession;

public class TransactionManager  implements MethodInterceptor {
    //入侵的方法
    public Object invoke(MethodInvocation arg0) throws Throwable {
        //调用目标方法
        boolean result = (Boolean)arg0.proceed();
        SqlSession session =  SqlSessionUtils.openSession();
        System.out.println("sqlSession===>"+session);
        if(result) {
            session.commit();
            System.out.println("提交事务");
        }else {
            session.rollback();
            System.out.println("回滚事务");
        }
        return result;
    }

}